package com.songshan.service;

import com.songshan.bean.User;
import com.songshan.dao.UserMapper;
import com.songshan.exception.BusinessException;
import com.songshan.filter.JWTUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginService {

  private Logger log = LoggerFactory.getLogger(this.getClass());

  @Autowired
  private UserMapper userMapper;

  public User login(String username, String password) throws BusinessException {
    User user = userMapper.selectUserByCode(username);
    if (!user.getPassword().equals(password)) {
      throw new BusinessException(500,"密码错误！");
    }
    if(user.getStatus()!=1){
      throw new BusinessException(500,"用户已禁用！");
    }
    String token = JWTUtil.generateToken(user);
    user.setToken(token);
    return user;
  }
}
